package com.galleryviewpager.i_wave_loading;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class PorterDuffModeDemoView extends View {

    private Paint mSrcPaint;
    private int mWidth, mHeight;
    private Paint mDstPaint;
    private PorterDuffXfermode mMode = new PorterDuffXfermode(PorterDuff.Mode.XOR);

    public PorterDuffModeDemoView(Context context) {
        this(context, null);
    }

    public PorterDuffModeDemoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PorterDuffModeDemoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        mSrcPaint = new Paint();
        mSrcPaint.setColor(Color.YELLOW);

        mDstPaint = new Paint();
        mDstPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (mWidthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = 400;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = 400;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawRect(20, 20, 80, 80, mDstPaint);

        mSrcPaint.setXfermode(mMode);
        canvas.drawCircle(30, 30, 30, mSrcPaint);
    }
}
